import { OptionsCallback } from '@/models/login'
import * as API from '@/service/tool/commentApi'
import { awaitTo } from '@/utils'
import { CommentTagModel } from '@/models/commentModels'

interface IState {
  tags: CommentTagModel[]
}

const commentHook = (options?: OptionsCallback) => {
  const state = reactive<IState>({
    tags: [],
  })

  /** 查询标签选项 */
  async function goodFeedbackTags() {
    const { result } = await awaitTo(API.goodFeedbackTags())
    state.tags = result?.data
  }

  /** 查询标签选项 */
  async function goodFeedbackList(page: number, perPage: number) {
    const { result } = await awaitTo(
      API.goodFeedbackList({
        page,
        per_page: perPage,
      }),
    )
    return result?.data.data
  }

  /** 查询标签选项 */
  async function submit(customerId: number, tags: string, images: string, onFinish?: () => void) {
    await uni.showLoading()
    const { result } = await awaitTo(
      API.submit({
        customer_id: customerId,
        tags,
        images,
      }),
    )
    uni.hideLoading()
    if (result) {
      onFinish()
    }
  }

  return {
    ...toRefs(state),
    goodFeedbackTags,
    goodFeedbackList,
    submit,
  }
}

export default commentHook
